package com.nsxy.common.config;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * Spring MVC配置
 * @author 潜行的青衣
 * @see
 */
@Order(10)
@Configuration
@PropertySource(value = {
		"classpath:config.properties"
})
@EnableWebMvc
@ComponentScan(basePackages = {
	"com.nsxy.**.controllers"
})
public class MVCConfig extends WebMvcConfigurerAdapter {
	
	@Autowired
	private Environment env;
	
	@Bean
	public ResourceBundleMessageSource messageSource() {
		ResourceBundleMessageSource source = new ResourceBundleMessageSource();
		source.setBasename(env.getProperty("mvc.message.base.name", "message"));
		source.setDefaultEncoding(env.getProperty("mvc.message.encoding", "UTF-8"));
		source.setUseCodeAsDefaultMessage(true);
		return source;
	}
	
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		String handlers = env.getProperty("mvc.resource.handlers", "/resources/**");
		String locations = env.getProperty("mvc.resource.locations", "/resources/");
		registry.addResourceHandler(handlers.split("\\|"))
			.addResourceLocations(locations.split("\\|"));
	}

	@Override
	public void configureMessageConverters(
			List<HttpMessageConverter<?>> converters) {
		converters.add(new StringHttpMessageConverter(Charset.forName(env.getProperty("http.message.convert.charset", "UTF-8"))));
		List<MediaType> supportedMediaTypes = new ArrayList<MediaType>();
		supportedMediaTypes.add(MediaType.TEXT_PLAIN);
		MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
		converter.setSupportedMediaTypes(supportedMediaTypes);
		converters.add(converter);
	}
	
	@Override
	public void configureViewResolvers(ViewResolverRegistry registry) {
		registry.jsp(
				env.getProperty("view.resolver.registry.prefix", "/WEB-INF/view/"), 
				env.getProperty("view.resolver.registry.suffix", ".jsp"));
	}

}
